package com.atguigu.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.atguigu.entity.*;
import com.atguigu.result.Result;
import com.atguigu.service.*;
import com.atguigu.vo.HouseQueryVo;
import com.atguigu.vo.HouseVo;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/house")
public class HouseController {
    @Reference
    private HouseService houseService;

    @Reference
    private CommunityService communityService;

    @Reference
    private DictService dictService;

    @Reference
    private HouseImageService houseImageService;

    @Reference
    private HouseBrokerService houseBrokerService;

    @Reference
    private UserFollowService userFollowService;



    @RequestMapping("/list/{pageNum}/{pageSize}")
    public Result list(@PathVariable Integer pageNum, @PathVariable Integer pageSize,@RequestBody HouseQueryVo houseQueryVo){
        PageInfo<HouseVo> pageInfo = houseService.findListPage(pageNum,pageSize,houseQueryVo);
        return Result.ok(pageInfo);
    }

    @RequestMapping("/info/{houseId}")
    public Result info(@PathVariable Long houseId, HttpServletRequest request){
        Map map=new HashMap<>();
        //获取房屋信息
        House house = houseService.getById(houseId);
        map.put("house",house);
        //获取小区信息
        Community community = communityService.getById(house.getCommunityId());
        map.put("community",community);
        //获取经纪人信息
        List<HouseBroker> houseBrokerList = houseBrokerService.findListByHouseId(houseId);
        map.put("houseBrokerList",houseBrokerList);
        //获取房屋图片信息
        List<HouseImage> houseImage1List = houseImageService.findList(houseId, 1);
        map.put("houseImage1List",houseImage1List);
        //判断该房屋是否已被当前用户关注
        boolean isFollow=false;
        //通过session获取当前用户
        UserInfo userInfo =(UserInfo) request.getSession().getAttribute("USER");
        if(userInfo!=null){
            //查表判断是否关注了该房屋
            boolean flag=userFollowService.isFollow(houseId,userInfo.getId());
            if(flag){
                isFollow=true;
            }
        }

        map.put("isFollow",isFollow);
        return Result.ok(map);
    }
}
